<?php
declare (strict_types=1);

namespace app\controller;

use app\facade\Param;
use app\handler\Excel;
use app\helpers\api\ApiResponse;
use app\model\Score;
use app\model\Userplan;
use think\Request;

class ScoreController
{
    use ApiResponse;

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $query = Score::order('id', 'desc');
        $query = Param::scoreParam($request->param(), $query);
        return $this->success($query->paginate($request->param('page_size', 15)));
    }

    public function show(Score $score)
    {
        return $this->success($score);
    }

    public function homeIndex(Request $request)
    {
        $validate = new \app\validate\Score();
        $res      = $validate->check($request->param());
        if (!$res) {
            return $this->error($validate->getError());
        }
        $query = Score::where('IDCard', $request->param('IDCard'))->order('id', 'desc');
        return $this->success($query->paginate($request->param('page_size', 15)));
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $exist = Score::where('batch', $request->param('batch'))->where('IDCard', $request->param('IDCard'))->findOrEmpty();
        if (!$exist->isEmpty()) {
            return $this->error('当前批次已经存在该人员');
        }
        Score::create($request->param());
        return $this->created();
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request, Score $score)
    {
        $score->update($request->param());
        return $this->update();
    }

    /**
     * 删除
     * @param Score $score
     * @return mixed
     */
    public function delete(Score $score)
    {
        $score->delete();
        return $this->destroy();
    }

    public function exportTemplate()
    {
        $datas = [
            [
                '批次ID',
                '费用',
                '考生姓名',
                '考生身份证号',
                '考生手机号',
                '岗位名称',
                '单位',
                '是否补考',
                '是否培训',
                '备注',
                '考试状态',
                '考试成绩',
                '通知模板ID',
                '考试开始时间',
                '考试结束时间',
            ]
        ];
        $excel = new Excel();
        $excel->exportExcel($datas, '成绩模板');
    }

    public function exportData(Request $request)
    {
        $key   = [
            '批次ID',
            '费用',
            '考生姓名',
            '考生身份证号',
            '考生手机号',
            '岗位名称',
            '单位',
            '是否补考',
            '是否培训',
            '备注',
            '考试状态',
            '考试成绩',
            '通知模板ID',
            '考试开始时间',
            '考试结束时间',
        ];
        $query = Score::field('batch,cost,studentName,IDCard,phone,post,companyName,makeup,training,remarks,examStatus,score,templateId,testStartTime,testEndTime')->order('id', 'desc');
        $query = Param::scoreParam($request->param(), $query);
        $datas = $query->select()->toArray();
        array_unshift($datas, $key);
        $excel = new Excel();
        $excel->exportExcel($datas, '成绩数据');
    }

    public function import(Request $request)
    {
        $file = request()->file('exl');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('exl', $file);
        $exl      = new Excel();
        $data     = $exl->importExecl(app()->getRootPath() . 'init/storage/' . $savename);
        unset($data[1]);
        foreach ($data as $k => $value) {
            $exist = Score::where('batch', $value['A'])->where('IDCard', $value['C'])->findOrEmpty();
            if (!$exist->isEmpty()) {
                continue;
            }
            Score::create([
                'batch'         => $value['A'],
                'cost'          => $value['B'],
                'studentName'   => $value['C'],
                'IDCard'        => $value['D'],
                'phone'         => $value['E'],
                'post'          => $value['F'],
                'companyName'   => $value['G'],
                'makeup'        => $value['H'],
                'training'      => $value['I'],
                'remarks'       => $value['J'],
                'examStatus'    => $value['K'],
                'score'         => $value['L'],
                'templateId'    => $value['M'],
                'testStartTime' => gmdate("Y/m/d h:i:s", \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['N'])),
                'testEndTime'   => gmdate("Y/m/d h:i:s", \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value['O'])),
            ]);
        }
        unlink(app()->getRootPath() . 'init/storage/' . $savename);
        return $this->created();
    }
}
